<template>
  <div class="course-recordings">

    <!-- 筛选区域 -->
    <div class="filter-area">
      <div class="filter-left">
        <el-select v-model="selectedTerm" placeholder="选择学期" class="filter-select" @change="loadClasses">
          <el-option
              v-for="term in terms"
              :key="term.termId"
              :label="term.termName"
              :value="term.termId"
          />
        </el-select>

        <el-select v-model="selectedClass" placeholder="选择班级" class="filter-select" @change="loadRecordings">
          <el-option
              v-for="cls in classes"
              :key="cls.classId"
              :label="cls.displayName"
              :value="cls.classId"
          />
        </el-select>
      </div>

      <div class="filter-right">
        <el-button type="primary" plain @click="handleManageReports">
          <el-icon><Document /></el-icon>
          管理报告
        </el-button>
        <el-button type="primary" @click="handleUploadRecording">
          <el-icon><Upload /></el-icon>
          上传实录
        </el-button>
      </div>
    </div>

    <!-- 课程列表 -->
    <div class="recording-list">
      <div v-for="recording in recordings" :key="recording.id" class="recording-item">
        <div class="recording-left">
          <el-image :src="recording.coverImage || '/images/default-recording.jpg'" alt="课程封面" class="recording-thumbnail"></el-image>
        </div>

        <div class="recording-right">
          <div class="recording-info">
            <h3 class="recording-title">{{ recording.title }}</h3>
            <div class="recording-meta">
              <span class="meta-item">
                <el-icon><Clock /></el-icon>
               {{ recording.activityDate }} {{ recording.startTime }} - {{ recording.activityDate }}  {{ recording.endTime }}
              </span>
              <span class="meta-item">
                <el-icon><User /></el-icon>
                {{ recording.className }}
              </span>
            </div>
            <p class="recording-desc">{{ recording.description }}</p>
          </div>

          <div class="recording-actions">
            <el-button type="primary" @click="playRecording(recording)">
              <el-icon><VideoPlay /></el-icon>
              播放实录
            </el-button>
            <el-button @click="viewReport(recording)">
              <el-icon><Document /></el-icon>
              查看报告
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-if="recordings.length === 0 && !loading" class="empty-state">
      <el-empty description="暂无课程实录" />
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <el-icon class="is-loading"><Loading /></el-icon>
      加载中...
    </div>

    <!-- 分页 -->
    <div v-if="recordings.length > 0" class="pagination">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          layout="prev, pager, next, jumper"
          @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import {
  Document,
  Upload,
  VideoPlay,
  Clock,
  User,
  Loading
} from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import {
  fetchAllTerms,
  fetchTeacherClasses,
  fetchRecordings,
  fetchExtracurricularRecordings
} from "@/api/teacher";

// 学期选项
const terms = ref([]);
const selectedTerm = ref('');

// 班级选项
const classes = ref([]);
const selectedClass = ref('');

// 课程实录数据
const recordings = ref([]);
const loading = ref(false);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 加载学期数据
const loadTerms = async () => {
  try {
    const res = await fetchAllTerms();
    if (res.success) {
      terms.value = res.data;
      if (terms.value.length > 0) {
        selectedTerm.value = terms.value[0].termId;
        await loadClasses();
      }
    }
  } catch (error) {
    ElMessage.error('加载学期数据失败');
  }
};

// 加载班级数据
const loadClasses = async () => {
  if (!selectedTerm.value) return;

  try {
    // 假设教师ID从用户信息获取，这里暂时用1
    const userInfoStr = localStorage.getItem('userInfo')
    const userInfo = JSON.parse(userInfoStr)
    const res = await fetchTeacherClasses(userInfo.id);

    if (res.success) {
      classes.value = res.data;
      if (classes.value.length > 0) {
        selectedClass.value = classes.value[0].classId;
        await loadRecordings();
      }
    }
  } catch (error) {
    ElMessage.error('加载班级数据失败');
  }
};

// 加载课程实录数据
const loadRecordings = async () => {
  if (!selectedTerm.value || !selectedClass.value) return;

  loading.value = true;
  try {
    const res = await fetchExtracurricularRecordings({
      termId:selectedTerm.value,
      classId:selectedClass.value});
    if (res.success) {
      recordings.value = res.data;
      total.value = recordings.value.length;
    }
  } catch (error) {
    ElMessage.error('加载课程实录失败');
  } finally {
    loading.value = false;
  }
};

// 格式化日期时间
const formatDateTime = (dateString) => {
  if (!dateString) return '未知时间';
  try {
    const date = new Date(dateString);
    return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    });
  } catch {
    return '未知时间';
  }
};

// 格式化时间
const formatTime = (dateString) => {
  if (!dateString) return '';
  try {
    const date = new Date(dateString);
    return date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    });
  } catch {
    return '';
  }
};

// 分页变化
const handlePageChange = (page) => {
  currentPage.value = page;
};

// 方法
const handleManageReports = () => {
  console.log('管理报告');
};

const handleUploadRecording = () => {
  console.log('上传实录');
};

const playRecording = (recording) => {
  console.log('播放实录:', recording.title);
  if (recording.videoUrl) {
    window.open(recording.videoUrl, '_blank');
  }
};

const viewReport = (recording) => {
  console.log('查看报告:', recording.title);
};

// 组件挂载时加载数据
onMounted(() => {
  loadTerms();
});
</script>


<style scoped>
.course-recordings {
  padding: 20px;
}

/* 筛选区域 */
.filter-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.filter-left {
  display: flex;
  gap: 15px;
}

.filter-select {
  width: 200px;
}

/* 课程列表 */
.recording-list {
  margin-bottom: 20px;
}

.recording-item {
  display: flex;
  gap: 20px;
  padding: 15px;
  margin-bottom: 15px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.recording-item:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.recording-thumbnail {
  width: 200px;
  height: 120px;
  object-fit: cover;
  border-radius: 4px;
}

.recording-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.recording-title {
  margin: 10px 0 10px 0;
  font-size: 18px;
  color: #303133;

}

.recording-meta {
  display: flex;
  gap: 20px;
  margin-bottom: 10px;
  color: #606266;
  font-size: 14px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.recording-desc {
  margin: 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.6;
}

.recording-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 10px;
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .filter-area {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .filter-left {
    flex-direction: column;
    width: 100%;
  }

  .filter-select {
    width: 100%;
  }

  .recording-item {
    flex-direction: column;
  }

  .recording-thumbnail {
    width: 100%;
    height: auto;
  }

  .recording-actions {
    justify-content: flex-start;
  }
}
</style>
